package com.czk.model.responsebilitychain.spring;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @Author:ChenZhangKun
 * @Date: 2021/10/27 22:13
 */
public abstract class AbstractHandler {
    /**
     * 下一个对象
     */
    private AbstractHandler nextHandler;
    public String name;

    public AbstractHandler getNextHandler() {
        return nextHandler;
    }

    public void setNextHandler(AbstractHandler nextHandler) {
        this.nextHandler = nextHandler;
    }

    public void filter(HttpServletRequest request, HttpServletResponse response) {
        boolean b = doFilter(request, response);
        if (!b){
            throw new RuntimeException(name+"校验失败");
        }
        // 传递
        if (getNextHandler() != null) {
            // 继续执行
            getNextHandler().filter(request, response);
        }
    }

    public abstract boolean doFilter(HttpServletRequest request, HttpServletResponse response);
    public abstract void setName(String name);
}
